package com.quwan.job;

import com.quwan.common.enums.JobTypeEnum;
import com.quwan.common.exception.QuWanException;
import com.quwan.job.impl.SpreadDataCountJobs;
import com.quwan.manager.controller.dto.JobLockDTO;
import com.quwan.manager.service.IJobLockService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author quan
 * @date 2021-07-27 14:36
 */

@Component
@EnableScheduling
@RequiredArgsConstructor
@Slf4j
public class QuartzHelper {
    private final SpreadDataCountJobs spreadDataCountJobs;

    private final IJobLockService iJobLockService;
    /**
     * 推广数据统计
     * cron 每天 23:50执行
     */
 //   @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Scheduled(cron = "0 50 23 * * ?")
    public void spreadDataJob() {
        log.info("################开始执行推广数据统计任务####################");
        JobLockDTO jobLockDTO =  iJobLockService.getVersion(JobTypeEnum.PROMOTE.getCode());
        try {
            int i = iJobLockService.updateVersion(JobTypeEnum.PROMOTE.getCode(), jobLockDTO.getVersion());
            if (i != 1) {
                return;
            }
            spreadDataCountJobs.Execute();
        } catch (Exception e) {
            e.getStackTrace();
            throw new QuWanException(e);
        }
        log.info("################执行推广数据统计任务结束####################");
    }

    /**
     * 留存报表数据统计
     * cron 每天 23:55执行
     */
    //@Scheduled(fixedDelay = 120000)
    @Scheduled(cron = "0 55 23 * * ?")
    public void preserveDataJob() {
        log.info("################开始留存报表数据统计任务####################");
        JobLockDTO jobLockDTO =  iJobLockService.getVersion(JobTypeEnum.RETAINED.getCode());
        try {
            int i = iJobLockService.updateVersion(JobTypeEnum.RETAINED.getCode(), jobLockDTO.getVersion());
            if (i != 1) {
                return;
            }
            spreadDataCountJobs.preserveDataExecute();
        } catch (Exception e) {
            e.getStackTrace();
            throw new QuWanException(e);
        }
        log.info("################执行留存报表数据统计任务结束####################");

    }

    /**
     * 财务报表数据统计
     * cron 每天 00:01 执行
     */
    //@Scheduled(fixedDelay = 120000)
    @Scheduled(cron = "0 01 0 * * ?")
    public void financeDataJob() {
        log.info("################开始财务报表数据统计任务####################");
        JobLockDTO jobLockDTO =  iJobLockService.getVersion(JobTypeEnum.FINANCE.getCode());
        try {
            int i = iJobLockService.updateVersion(JobTypeEnum.FINANCE.getCode(), jobLockDTO.getVersion());
            if (i != 1) {
                return;
            }

            spreadDataCountJobs.financeDataExecute();

        } catch (Exception e) {
            e.getStackTrace();
            throw new QuWanException(e);
        }
        log.info("################执行财务报表数据统计任务结束####################");

    }


    /**
     * 凌晨6点执行 缓存消息记录同步mysql
     */
    @Scheduled(cron = "0 02 6 * * ?")
    public void chatMsgSync() {
        log.info("################开始聊天消息记录同步任务####################");
        JobLockDTO jobLockDTO =  iJobLockService.getVersion(JobTypeEnum.MSG_SYNC.getCode());
        try {
            int i = iJobLockService.updateVersion(JobTypeEnum.MSG_SYNC.getCode(), jobLockDTO.getVersion());
            if (i != 1) {
                return;
            }
            spreadDataCountJobs.syncChatMsgExecute();
        } catch (Exception e) {
            e.getStackTrace();
            throw new QuWanException(e);
        }
        log.info("################执行聊天消息记录同步任务结束####################");

    }
}